package com.xc.freeapp.account.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Bookingreg;
import com.xc.freeapp.entity.Bookings;
import com.xc.freeapp.entity.Hospital;
import com.xc.freeapp.entity.Regpatient;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.request.bean.BookingregRegpatient;
import com.xc.freeapp.service.BookingregService;
import com.xc.freeapp.service.HospitalService;
import com.xc.freeapp.service.RegpatientService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.JsonUtil;
import com.xc.freeapp.util.TokenAnnotation;

/**
 * 预约挂号相关信息controller
 * */

@Controller
@RequestMapping("v2/bookingreg")
@Api(value = "v2bookingreg",description="预约挂号")
public class BookingRegControllerV2 extends AbstractController{
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private BookingregService    bookingregservice;
	@Autowired
	private RegpatientService regpatientServiceImpl;
	
	@Autowired
	private HospitalService hospitalService;

	/**
	 * 我的预约
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取我的预约",tags="我的预约")
	public Map<String,Object> getAppointments(@RequestParam(value = "page", required = false, defaultValue="1") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue="15") Integer limit)
			throws BaseException {
		Map<String,Object> responseMap=new HashMap<String, Object>();
		Map<String,Object> statisticsMap=new HashMap<String, Object>();
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		PageBounds pageBounds =new PageBounds(page, limit);
		List<Bookings> bookingslist = bookingregservice.queryAppointment(authInfo.getUserIntId(), pageBounds);
		Paginator paginator =((PageList)bookingslist).getPaginator();
		int invalidCount=0;
		for (Bookings bookings : bookingslist) {
			if("t".equals(bookings.getDelflag())){
				invalidCount++;
			}
		}
		statisticsMap.put("invalid",bookingslist.size()-invalidCount);
		statisticsMap.put("valid", invalidCount);
		responseMap.put("bookings", bookingslist);
		responseMap.put("paginator", paginator);
		responseMap.put("statistics", statisticsMap);
		return responseMap;
	}
	/**
	 * 取消预约
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "取消预约")
	public ResponseBean cancelAppointment(@RequestBody Map<String,Object> map)throws BaseException {

	 String id =map.get("id").toString();
		int f = bookingregservice.cancelAppointment(
				Integer.parseInt(id), "c");
		if (f > 0) {
			return ResponseBean.getSuccess();
		} else {
			throw new BaseException(Constant.GLOBAL_ERROR);
		}
	}
	/**
	 * 删除预约
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.DELETE)
	@ResponseBody
	@ApiOperation(value = "删除预约")
	public ResponseBean delAppointment(@RequestParam("id") String regbookingId)throws BaseException {

	
		int f = bookingregservice.deleteByPrimaryKey(Integer.parseInt(regbookingId));
		if (f > 0) {
			return ResponseBean.getSuccess();
		} else {
			throw new BaseException(Constant.GLOBAL_ERROR);
		}
	}

	/**
	 * 保存预约挂号
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "BookingRegAndPaitent", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存预约挂号和就诊人")
	public  Map<String,Object> saveBookingReg(@RequestBody BookingregRegpatient bookingregRegpatient) throws BaseException,Exception{
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
        Bookingreg bookingreg = bookingregRegpatient.getBookingreg();
        bookingreg.setUserid(authInfo.getUserIntId());
        CheckUtils.notNull(Constant.VER_NOTNULL, bookingreg);
        Hospital hospital = hospitalService.selectByPrimaryKey(authInfo.getAppIntId());
        Regpatient regpatient = bookingregRegpatient.getPatient();
        CheckUtils.notNull(Constant.VER_NOTNULL, regpatient);
        CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, regpatient.getTelphone(),"电话号码telphone");

		return bookingregservice.saveBookingRegAndPaitent(bookingreg, regpatient,hospital);
	}
	/**
	 * 保存预约挂号
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存预约挂号")
	public  Bookingreg saveBookingReg(@RequestBody @Valid Bookingreg bookingreg) throws BaseException,Exception{
		if(bookingreg.getPatientid()==null){
			throw new BaseException(Constant.ERRORCODE_905);
		}
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		bookingreg.setUserid(authInfo.getUserIntId());
		 Hospital hospital = hospitalService.selectByPrimaryKey(authInfo.getAppIntId());
		 Regpatient regpatient =regpatientServiceImpl.selectByPrimaryKey(bookingreg.getPatientid());
		bookingregservice.saveBookingRegV2(bookingreg,hospital,regpatient);
		return bookingreg;
	}
	/**
	 * 查询最近预约信息
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "/recently", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询最近预约信息")
	public  ResponsePage<List<Regpatient>> getBookingRegRecord(@RequestParam(value = "page", required = false, defaultValue="1") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue="100") Integer limit) throws BaseException,Exception{
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		PageBounds pageBounds =new PageBounds(page, limit);
        List<Regpatient>  regpatients  = regpatientServiceImpl.selectByUserId(authInfo.getUserIntId(), pageBounds);
        Paginator paginator =((PageList)regpatients).getPaginator();
		return new ResponsePage<List<Regpatient>>(regpatients, paginator);
	}
}
